/*******************************************************************************
 * AuctionsAction.java
 *
 * Created by Nishan
 * Last modified Date: Sep 12, 2008 3:49:11 PM by nishan
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.Items;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;

public class BuyAction extends FrontBaseSupport implements ServletRequestAware {

	private String subCategory;

	private List<GeneralCategoriesWOS> listGeneralCategories = new ArrayList<GeneralCategoriesWOS>();

	private List<Items> featuredItems = new ArrayList<Items>();

	public String getSubCategory() {
		return subCategory;
	}

	public List<GeneralCategoriesWOS> getListGeneralCategories() {
		return listGeneralCategories;
	}

	public BuyAction() {
		super(true, true);
		setCategoryDisplay(true);
	}

	public String buyAuctions() {
		setSubCategories();
		setCategoryHeader("category.menu.auction");

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 1));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String buyFixedPrice() {
		setSubCategories();
		setCategoryHeader("category.menu.fixPrice");

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 2));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String buyShops() {
		setSubCategories();
		setCategoryHeader("category.menu.shop");

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 3));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);


		return SUCCESS;
	}

	private HttpServletRequest request;

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	private void setSubCategories() {
		if(request.getSession().getAttribute("categoryInSession") != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("generalCategories.categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()));
			List<GeneralCategoriesWOS> tmpGeneralCategories = (List<GeneralCategoriesWOS>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategoriesWOS.class);

			boolean flag = false;
			for (GeneralCategoriesWOS generalCategories : tmpGeneralCategories) {

				if(flag) {
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("generalCategories.categoryId", generalCategories.getCategoryId());

					List<GeneralCategoriesWOS> tmpList = (List<GeneralCategoriesWOS>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategoriesWOS.class);

					if(tmpList.size() > 0) {
						for (GeneralCategoriesWOS generalCategoriesWOS : tmpList) {
							List list = CommonFunctionDelegator.executeSQLQuery("SELECT count(*) FROM items i, general_categories g where i.categoryid = g.categoryid and g.categoryId >= " + generalCategories.getCategoryId() + " and g.mainparentid = " + generalCategories.getMainParentId());
							Object objects = (Object) list.iterator().next();
							generalCategories.setCount(Integer.parseInt(objects.toString()));
						}
					} else {
						generalCategories.setCount(0);
					}
					generalCategories.setGeneralCategoriesList(tmpList);
					this.listGeneralCategories.add(generalCategories);
				} else {
					this.subCategory = generalCategories.getCategoryName();
					flag = true;
				}
			}
		}
	}

	public List<Items> getFeaturedItems() {
		return featuredItems;
	}
}
